/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 26727
 * Date: 2024-04-10
 * Time: 15:20
 */
class Solution4 {
    public int getMaxLen(int[] nums) {
        int n = nums.length;
        //1.创建dp表
        //f[i]表示以下标i为结尾的乘积正数的最长子数组长度
        //g[i]表示以下标i为结尾的乘积负数的最长子数组长度
        int[] f = new int[n+1];
        int[] g = new int[n+1];
        //2.初始化
        f[0] = 0;g[0] = 0;
        //3.状态转移方程
        int ret = Integer.MIN_VALUE;
        for(int i = 1; i <= n; i++) {
            if(nums[i-1] > 0) {
                f[i] = f[i-1]+1;
                g[i] = g[i-1] == 0 ? 0 : g[i-1]+1;
            }else if(nums[i-1] < 0) {
                f[i] = g[i-1] == 0 ? 0 : g[i-1]+1;
                g[i] = f[i-1]+1;
            }
            ret = Math.max(f[i],ret);
        }
        return ret;
    }
}
